module executor.authorize.client.AuthorizeExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import client.ImClient;
import std.array;
import command.StartAppCommand;
import std.conv: to;
import hunt.logging;
import core.stdc.time;
import client.MsgServerClient;
import core.stdc.string;
import hunt.serialization;

@TransportClient(logicClientName)
class AuthorizeExecutor : AbstractExecutor!(AuthorizeExecutor)
{
    @MessageId(MESSAGE.CMD_AUTHORIZE_RES)
    void authorize(TransportContext ctx, MessageBuffer buffer)
    {
        auto res = new AuthenticateRES();
        buffer.data.fromProtobuf!AuthenticateRES(res);

        TransportSession session = gateServer.sessionManager().get(res.clientId);
        logInfo("authorizeRes value: ", toJson(res));

        /**
        如果认证成功将用户信息保存到session
        如果重复登录向之前的连接发送重复登录包
        **/
        if (session !is null)
        {
            AuthenticateREQ authorize = cast(AuthenticateREQ)(session.getAttribute(SESSIONKEY.SRC));
            logInfo("authorizeRes code: %d, srcType: %d", res.code, authorize.src);
            if (res.code != ErrorCode.ERR_NONE || res.user is null)
            {
                logWarning("User auth error! close session!");
                session.send(new MessageBuffer(MESSAGE.CMD_AUTHORIZE_RES,res.toProtobuf.array));
                session.close();
                return;
            }
        
            TransportSession oldSesion = ImClient.getInstance().getImClientByMixId(ImClient.getInstance().idMixSrc(to!ulong(res.user.id), authorize.src));
            if(oldSesion !is null)
            {
                // ImClient.getInstance().deleClient(ImClient.getInstance().idMixSrc(to!ulong(res.user.id), authorize.src));
                    logErrorf("ImClient id(%s) by (%s) have exist! kick it!", res.user.id , authorize.src);
                    //repeatLogin repeatLoginReq = new repeatLogin;
                    //repeatLoginReq.timestamp = core.stdc.time.time(null);
                    //repeatLoginReq.src = authorize.src;
                    //oldSesion.send(new MessageBuffer(MESSAGE.CMD_REPEAT_LOGIN,repeatLoginReq.toProtobuf.array));
                    //oldSesion.close();
                    oldSesion.send(new MessageBuffer(MESSAGE.CMD_AUTHORIZE_RES,res.toProtobuf.array));
                    return;
            }
            else
            {
                logInfo("set attribute SESSIONKEY.USER userId: ", toJson(res.user));
                session.setAttribute(SESSIONKEY.USER, res.user);
                session.setAttribute(SESSIONKEY.LOGINMSG, res);
                MessageTransportClient msgServerClient = MsgServerClient.getInstance().getForwardClient(res.user.id);
                if (msgServerClient !is null)
                {
                        UserStatus userStatus = new UserStatus;
                        userStatus.userId    = to!string(res.user.id);
                        userStatus.src = authorize.src;
                        logInfo("authorizeRes code: %d",     res.code);
                        msgServerClient.send(new MessageBuffer(MESSAGE.CMD_AUTHORIZE_REQ,userStatus.toProtobuf.array));
                }
                ImClient.getInstance().addForwardClient(session,to!uint(res.user.id),authorize.src);
            }


            //ServerExtend serverExtend ;//= cast(ServerExtend)buffer.extend;
            //memcpy(&serverExtend,buffer.extend.ptr,ServerExtend.sizeof);
            //ClientExtend clientExtend;
            //clientExtend.tagId = serverExtend.tagId;
            //
            //ubyte[ClientExtend.sizeof] extend;
            //memcpy(extend.ptr,&clientExtend,ClientExtend.sizeof);

            session.send(new MessageBuffer(MESSAGE.CMD_AUTHORIZE_RES,res.toProtobuf.array));
        }
        else
        {
            logWarning("client id :%s is closed", res.clientId);
        }
    }
}

